package spring.cloud.tasks.executor.name_space.executor.task.task_item;

import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.common.node.TaskNodePath;
import spring.cloud.tasks.executor.common.Service;
import spring.cloud.tasks.executor.name_space.executor.task.TaskExecutor;
import spring.cloud.tasks.executor.name_space.executor.tasks_runtime_info.task_item_execute.TaskItemExecuteRenewalInfo;
import spring.cloud.tasks.executor.name_space.executor.tasks_runtime_info.task_item_execute.TaskItemExecuteRuntimeContext;
import spring.cloud.tasks.executor.name_space.executor.tasks_runtime_info.task_item_execute.TasksExecuteRuntimeContext;
import spring.cloud.tasks.executor.utils.LogUtils;

@Slf4j
@Setter
@Getter
public class TaskItemExecuteInfoService extends Service {
    private TasksExecuteRuntimeContext tasksExecuteContext;

    public TaskItemExecuteInfoService(TaskExecutor taskExecutor) {
        super(taskExecutor);

    }

    public void registerTaskItemExecuteRuntimeContext(TaskItemExecuteRuntimeContext taskItemExecuteRuntimeContext) {
        try {
            TaskItemExecuteRenewalInfo taskItemExecuteRenewalInfo = new TaskItemExecuteRenewalInfo();
            String taskInstanceAndTaskItemNode = TaskItemNode.getTaskItemNode(taskItemExecuteRuntimeContext.getTaskInstanceId(), taskItemExecuteRuntimeContext.getTaskItemIndex());
            String taskInstanceAndTaskItemNodePath = TaskNodePath.getTaskNodePath(taskId, taskInstanceAndTaskItemNode);
            this.nameSpaceDataManager.createIfNeedOrSetData(taskInstanceAndTaskItemNodePath, JSON.toJSONString(taskItemExecuteRenewalInfo));
        } catch (RuntimeException e) {
            LogUtils.warn(log, taskId, "update job return fail.", e);
        }
    }

    public void renewal(TaskItemExecuteRuntimeContext taskItemExecuteRuntimeContext, TaskItemExecuteRenewalInfo taskItemExecuteRenewalInfo) {
        //获取ZK续期信息，的标记是不是自己taskItemInstanceId,如果是则续期
        try {
            String executorId = taskItemExecuteRuntimeContext.getExecutorId();
            String taskItemInstanceId = taskItemExecuteRuntimeContext.getTaskItemInstanceId();
            String taskInstanceAndTaskItemNode = TaskItemNode.getTaskItemNode(taskItemExecuteRuntimeContext.getTaskInstanceId(), taskItemExecuteRuntimeContext.getTaskItemIndex());
            String taskInstanceAndTaskItemNodePath = TaskNodePath.getTaskNodePath(taskId, taskInstanceAndTaskItemNode);
            String data = this.nameSpaceDataManager.getData(taskInstanceAndTaskItemNodePath);
            TaskItemExecuteRenewalInfo $taskItemExecuteRenewalInfo = JSON.parseObject(data, TaskItemExecuteRenewalInfo.class);
            if (!$taskItemExecuteRenewalInfo.getExecutorId().equals(executorId) || !$taskItemExecuteRenewalInfo.getTaskInstanceId().equals(taskItemInstanceId)) {
                taskItemExecuteRuntimeContext.needStop = true;
                taskItemExecuteRuntimeContext.setNeedStopMessage("续期失败");
                return;
            }
            this.nameSpaceDataManager.createIfNeedOrSetData(taskInstanceAndTaskItemNodePath, JSON.toJSONString(taskItemExecuteRenewalInfo));
        } catch (Exception e) {
            //要是续期失败了 慢慢等下次续期机会 直到十分钟后还续期不成功那就不管了
            log.error("renewal", e);
        }
    }
}
